package com.nanyang.tms.service.waybill.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.waybill.*;
import com.nanyang.tms.api.dto.response.forecast.ForecastReceiverResponse;
import com.nanyang.tms.api.dto.response.forecast.ForecastSenderResponse;
import com.nanyang.tms.api.dto.response.waybill.*;
import com.nanyang.tms.api.service.waybill.WaybillRpcService;
import com.nanyang.tms.dao.waybill.WaybillDao;
import com.nanyang.tms.dao.waybill.WaybillPayFeeDao;
import com.nanyang.tms.dao.waybill.WaybillReceiptVolumeDao;
import com.nanyang.tms.dao.waybill.WaybillShipmentVolumeDao;
import com.nanyang.tms.domain.waybill.*;
import com.nanyang.tms.manager.waybill.WaybillIncomeFeeManager;
import com.nanyang.tms.manager.waybill.WaybillManager;
import com.nanyang.tms.manager.waybill.WaybillPayFeeManager;
import com.nanyang.tms.manager.waybill.WaybillTrackManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class WaybillRpcServiceImpl implements WaybillRpcService {

    @Autowired
    private WaybillManager waybillManager;
    @Autowired
    private WaybillTrackManager waybillTrackManager;
    @Autowired
    private WaybillPayFeeManager waybillPayFeeManager;
    @Autowired
    private WaybillPayFeeDao waybillPayFeeDao;
    @Autowired
    private WaybillIncomeFeeManager waybillIncomeFeeManager;
    @Autowired
    private WaybillDao waybillDao;
    @Autowired
    private WaybillReceiptVolumeDao waybillReceiptVolumeDao;
    @Autowired
    private WaybillShipmentVolumeDao waybillShipmentVolumeDao;


    @Override
    public ApiResult<PageBean<WaybillPageResponse>> listWaybill(WaybillQueryRequest param) {
        ApiResult<PageBean<WaybillPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            WaybillQuery query = new WaybillQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<WaybillVO> waybillList = waybillManager.listWaybill(query);
            PageBean<WaybillPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(waybillList.getTotalItem());
            BeanUtils.copyListProperties(waybillList.getData(), resultList.getData(), WaybillPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @Override
    public ApiResult<PageBean<WaybillTrackPageResponse>> listWaybillTrack(WaybillTrackQueryRequest param) {
        ApiResult<PageBean<WaybillTrackPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            WaybillTrackQuery query = new WaybillTrackQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<WaybillTrackVO> waybillTrackList = waybillTrackManager.listWaybillTrack(query);
            PageBean<WaybillTrackPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(waybillTrackList.getTotalItem());
            BeanUtils.copyListProperties(waybillTrackList.getData(), resultList.getData(), WaybillTrackPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
    @Override
    public ApiResult<PageBean<WaybillPayFeePageResponse>> listWaybillPayFee(WaybillPayFeeQueryRequest param) {
        ApiResult<PageBean<WaybillPayFeePageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            WaybillPayFeeQuery query = new WaybillPayFeeQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<WaybillPayFeeVO> waybillPayFeeList = waybillPayFeeManager.listWaybillPayFee(query);
            PageBean<WaybillPayFeePageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(waybillPayFeeList.getTotalItem());
            BeanUtils.copyListProperties(waybillPayFeeList.getData(), resultList.getData(), WaybillPayFeePageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
    @Override
    public ApiResult<PageBean<WaybillIncomeFeePageResponse>> listWaybillIncomeFee(WaybillIncomeFeeQueryRequest param) {
        ApiResult<PageBean<WaybillIncomeFeePageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            WaybillIncomeFeeQuery query = new WaybillIncomeFeeQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<WaybillIncomeFeeVO> waybillIncomeFeeList = waybillIncomeFeeManager.listWaybillIncomeFee(query);
            PageBean<WaybillIncomeFeePageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(waybillIncomeFeeList.getTotalItem());
            BeanUtils.copyListProperties(waybillIncomeFeeList.getData(), resultList.getData(), WaybillIncomeFeePageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @Override
    public ApiResult<Boolean> completeState(WaybillPayFeeStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            WaybillPayFeeDO waybillPayFeeDO = new  WaybillPayFeeDO();
            BeanUtils.copyProperties(param,waybillPayFeeDO);
            waybillPayFeeDO.setGmtModifiedPin(param.getPin());
            waybillPayFeeDao.completeState(waybillPayFeeDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<WaybillReceiverSenderResponse> waybillReceiverSender(String waybillCode) {
        ApiResult<WaybillReceiverSenderResponse> result = new ApiResult<>();
        try {
            WaybillReceiverSenderResponse waybillReceiverSenderResponse = new WaybillReceiverSenderResponse();
            WaybillReceiverSenderVO waybillReceiverSenderVO = waybillManager.receiverSenderByWaybillCode(waybillCode);
            ForecastReceiverResponse forecastReceiverResponse = new ForecastReceiverResponse();
            BeanUtils.copyProperties(waybillReceiverSenderVO.getForecastReceiverVO(),forecastReceiverResponse);
            waybillReceiverSenderResponse.setReceiverResponse(forecastReceiverResponse);

            ForecastSenderResponse forecastSenderResponse=new ForecastSenderResponse();
            BeanUtils.copyProperties(waybillReceiverSenderVO.getForecastSenderVO(),forecastSenderResponse);
            waybillReceiverSenderResponse.setSenderResponse(forecastSenderResponse);

            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(waybillReceiverSenderResponse);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<List<WaybillReceiptVolumeResponse>> listWaybillReceiptVolume(String waybillCode) {
        ApiResult<List<WaybillReceiptVolumeResponse>> result = new ApiResult<>();
        try {
            List<WaybillReceiptVolumeResponse>  responseList = new ArrayList<>();
            List<WaybillReceiptVolumeVO> waybillReceiptVolumeVOS = waybillReceiptVolumeDao.selectByCode(waybillCode);
            if(CollectionUtil.isNotEmpty(waybillReceiptVolumeVOS)){
                BeanUtils.copyListProperties(waybillReceiptVolumeVOS,responseList,WaybillReceiptVolumeResponse.class);
            }
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(responseList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<List<WaybillShipmentVolumeResponse>> listWaybillShipmentVolume(String waybillCode) {
        ApiResult<List<WaybillShipmentVolumeResponse>> result = new ApiResult<>();
        try {
            List<WaybillShipmentVolumeResponse>  responseList = new ArrayList<>();
            List<WaybillShipmentVolumeVO> waybillShipmentVolumeVOS = waybillShipmentVolumeDao.selectByCode(waybillCode);
            if(CollectionUtil.isNotEmpty(waybillShipmentVolumeVOS)){
                BeanUtils.copyListProperties(waybillShipmentVolumeVOS,responseList,WaybillShipmentVolumeResponse.class);
            }
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(responseList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @Override
    public ApiResult<Boolean> creatTrack(String  supplierNO) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            waybillTrackManager.creatTrack(supplierNO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @Override
    public ApiResult<Boolean> notifyTrack(String sign, String body) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            waybillTrackManager.notifyTrack(sign,body);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<WaybillDetailResponse> queryDetail(String waybillCode) {
        ApiResult<WaybillDetailResponse> result = new ApiResult<>();
        try {
            WaybillDetailResponse  waybillDetailResponse = new WaybillDetailResponse();
            WaybillVO waybillVO = waybillDao.queryDetail(waybillCode);
            if(ObjectUtil.isNotEmpty(waybillVO)){
                BeanUtils.copyProperties(waybillVO,waybillDetailResponse);
            }
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(waybillDetailResponse);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
